<?php
// +------------------------------------------------------------------------+
// | PHP Melody
// +------------------------------------------------------------------------+
// | PHP Melody IS NOT FREE SOFTWARE
// | If you have downloaded this software from a website other
// | than www.phpsugar.com or if you have otherwise received
// | this software from someone who is not a representative of
// | this site you are involved in an illegal activity.
// | ---
// | In such case, please contact us at: support@phpsugar.com.
// +------------------------------------------------------------------------+
// | Developed by: phpSugar (www.phpsugar.com) / support@phpsugar.com
// | Copyright: (c) 2004-2012 PhpSugar.com. All rights reserved.
// +------------------------------------------------------------------------+

@set_time_limit(0);
$showm = '1';
include('header.php');

?>

<div id="adminPrimary">
	<div class="content">
		
		 <h2>Update from version 2.0 to 2.0.1</h2>
		 
		 <div class="row-fluid">
			<?php if ($_POST['Update'] == '') : ?>
			<form name="update-database" method="POST" action="db_update.php">
            <div class="well">
			<p>Press the 'Update' button below if you're ready to update to version 2.0.1.<br />
			This process should take only a few seconds.
			</p>
            </div>
				<?php if ($config['version'] != '2.0' && $config['version'] != '2.0.1') : ?>
				
					<div class="alert alert-warning">
						Warning! This update works only with version <strong>2.0</strong>. This site is currently uses version <strong><?php echo $config['version']; ?></strong>. Log into your customer area account to download any required updates.
					</div>
				
				<?php elseif ($config['version'] == '2.0.1') : ?>
				
					<div class="alert alert-success">
						Looks like your database is already up to date. There's no need to start this process again. 
						<br /> 
						Please delete <code>/admin/db_update.php</code>.
					</div>
				
				<?php else: ?>
				
					<button type="submit" name="Update" value="Update" class="btn btn-blue" onclick="javascript: $('.importLoader').css({'display' : 'inline'});">Update</button> <em class="importLoader"><small>Please wait...</small></em>
				
				<?php endif;?>
			</form>
			<?php elseif ($_POST['Update'] != ''): 
				
				// OK, let's get the engine started...
				$sql = array();
				$errors = array();
				
				add_config('user_upload_daily_limit', 20);
				
				
				// ----- --- ------ ---- old: 
				
				$sql[] = "UPDATE pm_sources 
SET embed_code = '<object classid=\"clsid:D27CDB6E-AE6D-11cf-96B8-444553540000\" codebase=\"http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,40,0\" width=\"%%player_w%%\" height=\"%%player_h%%\">
	<param name=\"movie\" value=\"http://www.youtube.com/v/%%yt_id%%?hl=en_US&fs=1&hd=%%use_hq_vids%%&rel=0&autoplay=%%player_autoplay%%&color2=0x%%player_bgcolor%%&showsearch=0&showinfo=0&iv_load_policy=3\">
	</param><param name=\"allowFullScreen\" value=\"true\">
	</param><param name=\"allowscriptaccess\" value=\"never\">
	</param><param name=\"allowNetworking\" value=\"internal\">
	</param><param name=\"wmode\" value=\"%%player_wmode%%\">
	</param>
	<embed src=\"http://www.youtube.com/v/%%yt_id%%?hl=en_US&fs=1&hd=%%use_hq_vids%%&rel=0&autoplay=%%player_autoplay%%&color2=0x%%player_bgcolor%%&showsearch=0&showinfo=0&iv_load_policy=3\" type=\"application/x-shockwave-flash\" allowscriptaccess=\"never\" allowNetworking=\"internal\" allowfullscreen=\"true\" wmode=\"%%player_wmode%%\" width=\"%%player_w%%\" height=\"%%player_h%%\">
	</embed>
</object>' 
WHERE source_name = 'youtube'";
				
				
				$sql_count = count($sql);
			
				for ($i = 0; $i < $sql_count; $i++)
				{
					$result =  mysql_query($sql[ $i ]);
					if ( ! $result)
					{
						if( ! preg_match("/Duplicate column name/i", mysql_error()) )
						{
							$errors[$i] = mysql_error();
							$errs++;
						}
					}
				}
				unset($sql);
			
				
				if (count($errors) == 0)
				{
					update_config('version', '2.0.1');
					
					// Clear SMARTY cache
					$dir = @opendir($smarty->compile_dir);
					if ($dir)
					{
						while (false !== ($file = readdir($dir)))
						{
							if(strlen($file) > 2)
							{
								$tmp_parts = explode('.', $file);
								$ext = array_pop($tmp_parts);
								$ext = strtolower($ext);
								if ($ext == 'php' && strpos($file, '%') !== false)
								{
									@unlink($smarty->compile_dir .'/'. $file);
								}
							}
						}
						@closedir($dir);
					}
					
					//	empty cache directory
					$dir = @opendir($smarty->cache_dir);
					if ($dir)
					{
						while (false !== ($file = readdir($dir)))
						{
							if(strlen($file) > 2)
							{
								$tmp_parts = explode('.', $file);
								$ext = array_pop($tmp_parts);
								$ext = strtolower($ext);
								if ($ext == 'php' && strpos($file, '%') !== false)
								{
									@unlink($smarty->cache_dir .'/'. $file);
								}
							}
						}
						@closedir($dir);
					}
					
					?>
		            <div class="alert alert-success">
		            Your database has been successfully updated to version 2.0.1. Continue to your <a href="index.php">Dashboard</a>.
		            </div>            
		            <?php
					
					if ( ! @unlink("db_update.php")) 
					{
					?>
					 <div class="alert alert-warning">
					 <strong>Notice:</strong> For safety reasons, please delete <code>/admin/db_update.php</code> right now.
					 </div>
					<?php
					}
				}
				else 
				{
					?>
					<div class="alert alert-danger">
					<ul>
					<?php
					// display errors
					foreach ($errors as $query_number => $sql_error)
					{
						?>
						<li>
							An error occured on query #<?php echo $query_number;?>: <em><?php echo $sql_error;?></em>
						</li>
						<?php 
					}
					?>
					</ul>
					</div>
					<?php
				}

			endif;
			?>
		 </div><!-- .row-fluid -->
    </div><!-- .content -->
</div><!-- .primary -->
<?php
include('footer.php');
?>		 